package zd.cornermemory.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import zd.cornermemory.R;
import zd.cornermemory.db.Cj;
import zd.cornermemory.utils.Statistics;

import static zd.cornermemory.R.id.code;

public class PjCjAdapter extends BaseAdapter {
	private Context        mContext;
	private List<Cj>       cjs;
	private LayoutInflater mInflater;

	final class ViewHolder {
		public TextView cj,code;
	}

	public PjCjAdapter(Context context, List<Cj> cjs) {
		this.mContext = context;
		this.mInflater = LayoutInflater.from(context);
		this.cjs = cjs;
	}

	@Override
	public int getCount() {
		return cjs==null?0:cjs.size();
	}

	@Override
	public Object getItem(int arg0) {

		return cjs==null?null:cjs.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			int resId = R.layout.pj_cj_item;
			convertView = mInflater.inflate(resId, null);
			holder.cj = (TextView) convertView.findViewById(R.id.sj);
			holder.code = (TextView) convertView.findViewById(code);
			convertView.setTag(holder);
		} else{
			holder = (ViewHolder) convertView.getTag();
		}

		Cj cj = cjs.get(position);
		String code = cj.getCode();
		String time = Statistics.timeToString(cj.getSingTime().intValue());
		switch (cj.getStatus()) {
			case 0:
				break;
			case 1://+2
				time += "+";
				break;
			case 2:
				time = "DNF"+" ("+time+")";
				break;

		}

		holder.cj.setText(position+1+".  "+time);
		holder.code.setText(code);
		return convertView;
	}
}
